home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: Fastest way to index thru an array????
- Date: 11 Jan 1996 15:26:00 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan11102600@g7240065.bridge.bst.bls.com>
- References: <4d1g3k$o09@solaris.cc.vt.edu>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Ashutosh Gokhale's message of 10 Jan 1996 22:54:44 GMT
-
- In article <4d1g3k$o09@solaris.cc.vt.edu> Ashutosh Gokhale <ashutosh> writes:
-
- : Suppose I have an array A[][][] which I declare as
- : double ***A and then assign memory to it using new operator.
- : Now consider that A has dimensions A[50][60][70]. Then I can index thru ALL
- : entries of A using
- : for(i=1; i<50; i++)
- : {
- : for(j=1; j<60; j++)
- : {
- : for(k=1; k<70; k++)
- : {
- : A[i][j][k] = <some expression>;
- : }
- : }
- : }
- : But the above way is surely the most time-INEFFICIENT way.
- : Can someone suggest me the MOST time efficient way of indexing through A[][][]?
-
- The only inefficient bit is the array access, the actual looping touches
- every location only once and you can't really get a more efficient access
- than that.
- You can change the array access to pointer access and do something like
- (NB: array indices start at 0)
-
- double*** fred = A;
- for (int i = 0; i < 50; i++) {
- double** wilma = *fred++;
- for (int j = 0; j < 60; j++) {
- double* dino = *wilma++;
- for (int k = 0; k < 70; k++) {
- *dino++ = <some expression>;
- }
- }
- }
-
- But this may be totally unnecessary because the compiler could optimise this
- code for you.
-
- Regards
- -A.
-
- --
- | A.Champion |
-